//TODO: dorobit komentare

package sk.robocup.dvanastyhrac.tactics;

import robocup.component.SConf;
import robocup.component.actions.Action;
import robocup.component.actions.ChangeViewModeAction;
import robocup.component.actions.KickAction;
import robocup.component.actions.TurnNeckAction;
import robocup.component.geometry.Vektor;
import robocup.component.tactics.StateEvaluation;

/**
 * Stav rozsiruje povodny stav nahravky "PassStateWithSay12". Pouziva sa jedine
 * pri vykopavani brankara alebo autovom vhadzovani hraca. Hrac najde najlepsii
 * smer pre vhodenie lopty tak, aby bol nas hrac pri lopte najblizsie, prip. aby
 * bolo co najmesie ohrozenie nasej branky z nasledneho utoku.*
 *
 * Created on 24.4.2009
 * @author mpolak
 */
public class KickInStateWithSay12 
        extends PassStateWithSay12
{

    /*
     * @param parent Evaluator ktory vyhodnocuje prinos taktik.
     */
    public KickInStateWithSay12(StateEvaluation parent) {
        super(parent);
        this.state = STATES.PLAYER_KICK_IN;
    }

    /**
     * Hrac musi byt pri lopte (stav hry musi byt KICK_IN_OWN)
     * @return boolean
     */

    @Override
    public boolean preCondition()
    {
        if(!checkPlayMode())
            return false;

        Vektor ballPos = getWorld().getBallRef().getPosition();
        if(ballPos == null)
            return false;

        return this.checkMyBall();
    }

    @Override
    protected double successBenefit() {
        return 0.7D;
    }

    @Override
    protected double successProbability() {
        return 1.0D;
    }




    /**
     * vypocita hlavnu akciu - nahravku nejakemu hracovi ak je mozna, ak nie je,
     * nahravku do volneho priestoru pod urcitym uhlom
     */
    @Override
	public void calculateMainAction()
    {
        //debug> System.out.println("KickIn action - "+getPlayer().getNumber()+": Som v stave prihravky autu...vyberam akciu");
		    mainAction = this.getProphet().getPassAction();
        
        
        //ak nevyberie akciu, tj. mainAction == null, kopni do volneho priestoru
        if(mainAction==null)
            mainAction = kickInFreeSpace();

	}

    @Override
    public TurnNeckAction getTurnNeckAction() {
        return super.getTurnNeckAction();
    }

    @Override
    public ChangeViewModeAction getViewModeAction() {
        return super.getViewModeAction();
    }

    //TODO: Upravit, zlepsit...
    private Action kickInFreeSpace()
    {
        // zatial len kopni pod smerom k brane
        Action retVal = null;
        double angle = 0;

        double posX = this.getWorld().getSelfRef().getPosition().getX();
        double posY = this.getWorld().getSelfRef().getPosition().getY();

        //ak sme na nasej polke, kopeme popri autovej ciare
        if(posY<0.0)
        {
            if(posX<0.0) angle = 5.0;
            if(posX>0.0) angle = -5.0;


            // vypocitam uhol o ktory sa mam otocit - DAInamite sposob
            double angleToField = Vektor.normalize(SConf.getInstance().GOAL_POS_OTHER.sub(
                  this.getWorld().getMyNextPosition()).getAngle() - this.getWorld().getBodyAngle());

            angle = angleToField-angle;
        }
        else //kopeme smerom do stredu branky
        {
            // vypocitaj uhol medzi loptou a brankou
            angle = this.getWorld().getBallRef().getAngleTo(SConf.getInstance().GOAL_POS_OTHER);
        }

        //kopni pod tymto uhlom
        retVal = new KickAction(100, angle);

        //debug> System.out.println("Prihravam naivne pod uhlom: "+angle);
        
        return retVal;
    }



}
